﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spider.Models.Stock;

namespace Spider.DataAccess.Stock
{
    public class StockCompanyDataAccess
    {

        public void AddCompanies(IEnumerable<StockCompanyModel> companyList)
        {
            string sql = @"insert into StockCompany (
                                StockCode,
                                StockBourse,
                                CompanyName,
                                CompanyNature
                            ) values (
                                @StockCode,
                                @StockBourse,
                                @CompanyName,
                                @CompanyNature
                            )";
            SQLiteHelper.Instance.AppendTransaction();

            foreach (StockCompanyModel company in companyList)
            {
                object[] sqlParams = new object[]
                {
                   company.StockCode,
                   (int)company.StockBourse,
                   company.CompanyName,
                   company.CompanyNature
                };
                SQLiteHelper.Instance.ExecuteNonQuery(sql, sqlParams);
            }


            SQLiteHelper.Instance.CommitTransaction();
        }

        public List<StockCompanyModel> GetAllCompanies()
        {
            string sql = @"select StockCode,
                                StockBourse,
                                CompanyName,
                                CompanyNature 
                            from StockCompany";

            var reader = SQLiteHelper.Instance.ExecuteReader(sql);
            List<StockCompanyModel> companies = new List<StockCompanyModel>();
            while (reader.Read())
            {
                companies.Add(new StockCompanyModel()
                {
                    StockCode = reader["StockCode"].ToString(),
                    StockBourse = (BourseType)Convert.ToInt32(reader["StockBourse"]),
                    CompanyName = reader["CompanyName"].ToString(),
                    CompanyNature = reader["CompanyNature"].ToString()
                });
            }

            return companies;
        }
    }
}
